package com.example.videoproject.controller;



import com.example.videoproject.pojo.Person;
import com.example.videoproject.pojo.Users;
import com.example.videoproject.service.IPersonService;
import com.example.videoproject.service.IUsersService;
import com.example.videoproject.utils.Code;

import com.example.videoproject.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 二组
 * @since 2022-07-23
 */
@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private IUsersService usersService;

    @Autowired
    private IPersonService personService;

    //首页用户登录
    @PostMapping("login")
    public Result Login(Users user) {
        Users temp = usersService.getById(user.getUid());
        Integer code = 0;
        if (temp != null) {
            if (user.getPwd().equals(temp.getPwd().trim())) {
                //密码正确后返回用户信息
                Person person = personService.getById(user.getUid());
                if (person == null) {
                    return new Result(Code.SELECT_ERR,null);
                }else {
                    code = Code.SELECT_OK;
                    return new Result(code, person);
                }
            } else {
                code = Code.SELECT_ERR;
                return new Result(code, null);
            }
        }
        code = Code.SELECT_ERR;
        return new Result(code, null);
    }


    @RequestMapping("EnrollUser")
    public boolean EnrollUsers(Users users){
        users.setDeleted(0);
        users.setLevel(1);
        boolean rs = usersService.save(users);
        return rs;
    }


    @RequestMapping("ManagerLogin")
    public Result ManagerLogin(Users user) {

//        Users users1 = usersService.getById(users.getUid());
//        return users1;
        System.out.println(user);
        Users temp = usersService.getById(user.getUid());
        System.out.println(temp);
        Integer code = 0;
        if (temp != null) {
            if (user.getPwd().equals(temp.getPwd().trim())) {
                //密码正确后返回用户信息
                code = Code.SELECT_OK;
                return new Result(code, temp);
            } else {
                code = Code.SELECT_ERR;
                return new Result(code, null);
            }
        }
        code = Code.SELECT_ERR;
        return new Result(code, null);
    }



    @RequestMapping("changePwd")
    public boolean changePwd(Users users){
        users.setDeleted(0);
        return usersService.updateById(users);
    }

    //个人信息修改
    @PostMapping("update")
    public Result update(Person person){
        personService.updateById(person);
        Person person1 = personService.getById(person);
        return new Result(Code.UPDATE_OK,person1);
    }

    //修改密码
    @PostMapping("modify")
    public Result modify(Users user){
        usersService.updateById(user);
        return new Result(Code.UPDATE_OK,null);
    }

    @PostMapping("modifyTx")
    public Result modifyTx(@RequestParam("file")MultipartFile file){
        System.out.println(file.toString());
        return null;
    }

    private Object saveFile(MultipartFile file){
        if (file.isEmpty()){
            return "未选择文件";
        }
        String filename = file.getOriginalFilename(); //获取上传文件原来的名称
        String filePath = "/file/";
        File temp = new File(filePath);
        if (!temp.exists()){
            temp.mkdirs();
        }

        File localFile = new File(filePath+filename);
        try {
            file.transferTo(localFile); //把上传的文件保存至本地
            System.out.println(file.getOriginalFilename()+" 上传成功");
        }catch (IOException e){
            e.printStackTrace();
            return "上传失败";
        }

        return "ok";
    }

    @PostMapping("/delete")
    public boolean delete(String uid){
        boolean ret = usersService.removeById(uid);
        return ret;
    }
}

